package engine;

//Vecteur 2D en coordonnées homogenes
public class Vector2D {

    private Double x, y, w;

    public Vector2D(Double w) {
        this.w = w;
    }

    public Vector2D(Double x, Double y, Double w) {
        this.x = x;
        this.y = y;
        this.w = w;
    }

    public Double getX() {
        return x;
    }

    public Double getY() {
        return y;
    }

    public void setX(Double x) {
        this.x = x;
    }

    public void setY(Double y) {
        this.y = y;
    }

    public Double getW() {
        return w;
    }

    static public Vector2D add(Vector2D v1, Vector2D v2) {
        return new Vector2D(v1.x + v2.x, v1.y + v2.y, v1.w + v2.w);
    }

    static public Vector2D sub(Vector2D v1, Vector2D v2) {
        return new Vector2D(v1.x - v2.x, v1.y - v2.y, v1.w - v2.w);
    }
    
    static public Vector2D mul(Vector2D v1, Vector2D v2) {
        return new Vector2D(v1.x * v2.x, v1.y * v2.y, v1.w * v2.w);
    }
    
    static public Vector2D mul(Vector2D v1, Double scalar) {
        return new Vector2D(v1.x * scalar, v1.y * scalar, v1.w);
    }

    static public Vector2D div(Vector2D v1, Double scalar) {
        return new Vector2D(v1.x / scalar, v1.y / scalar, v1.w);
    }

    @Override
    public Vector2D clone() {
        return new Vector2D(x, y, w);
    }

    public Double norm() {
        return Math.sqrt(x * x + y * y);
    }

    public Vector2D normalize() {
        Double norm = norm();

        if (norm == 0.) {
            return this;
        }

        return new Vector2D(x / norm(), y / norm(), 0.);
    }
    
    public static Double dotProduct(Vector2D v1, Vector2D v2){
        return v1.x * v2.x + v1.y * v2.y;
    }
    
    //retourne l'angle géométrique formé par les deux vecteurs
    public static Double angleBetween(Vector2D v1, Vector2D v2){
        Double prodScalaire = dotProduct(v1, v2);
        if(prodScalaire == 0.) return 0.;
        
        Double cos = prodScalaire / (v1.norm() * v2.norm());
        
        Double pourLeSigne = v1.x*v2.y - v1.y*v2.x;
        Integer sign = (pourLeSigne >= 0) ? -1 : 1;
        
        return sign*Math.acos(cos);
    }
}
